import org.zkoss.zul.Area;
import org.zkoss.zul.Chart;
import org.zkoss.zul.Menupopup;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.SimpleCategoryModel;
import org.zkoss.zul.Window;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;

import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.OpenEvent;

import java.util.Map;
import java.util.Iterator;

import utilities.Pair;

public class CategoryChart extends Chart {
	private Area selectedArea;
	private SimpleCategoryModel catModel;
	private Map dataMap;
	private boolean onArea;
	
	/*  Constructor  */
	public CategoryChart(){
		super();
		
		catModel = new SimpleCategoryModel();
		setModel( catModel );
		
		addEventListener( "onClick", new EventListener(){                                     /*  Data dig  */
			public void onEvent( Event event ){
				Menupopup popup = ( Menupopup )getParent().getFellow( "DG_CPOP" );
				if( ( event.getArea() != null ) && ( getFellow( event.getArea() ).getAttribute( "value" ) != null ) ){ /*  Select right area  */
					selectedArea = getFellow( event.getArea() );
					onArea = true;
					( ( Menuitem )popup.getFellow( "DG_CPOP_DETAIL" ) ).setDisabled( false );
					( ( Menuitem )popup.getFellow( "DG_CPOP_INSERT" ) ).setDisabled( true );
					( ( Menuitem )popup.getFellow( "DG_CPOP_MODIFY" ) ).setDisabled( false );
					( ( Menuitem )popup.getFellow( "DG_CPOP_REMOVE" ) ).setDisabled( false );
				}else{
					selectedArea = null;
					onArea = false;
				}
			}
		} );
	}
	
	/*  Show detail popup menu  */
	public void detail(){
		final Window winDet = ( Window )Executions.createComponents( "components/CatDetail.zul", null, null );
		( ( Label )winDet.getFellow( "DG_DESER" ) ).setValue( selectedArea.getAttribute( "series" ) );
		( ( Label )winDet.getFellow( "DG_DECAT" ) ).setValue( selectedArea.getAttribute( "category" ) );
		( ( Label )winDet.getFellow( "DG_DEVAL" ) ).setValue( selectedArea.getAttribute( "value" ).toString() );
		winDet.doHighlighted();
		selectedArea = null;
	}
	
	/*  Show insert popup menu  */
	public void insertDialog(){
		final Window winIns = ( Window )Executions.createComponents( "components/CatInsert.zul", null, null );
		winIns.setSizable( false );
		winIns.setAttribute( "commander", this );
		winIns.doModal();
	}
	
	/*  Show remove popup window  */
	public void remove(){
		Messagebox msg = new Messagebox();
		Comparable category = selectedArea.getAttribute( "category" );
		Comparable series = selectedArea.getAttribute( "series" );
		if( msg.show( "您确定要删除" + series + " " + category + "吗?", "确认删除", Messagebox.YES + Messagebox.NO, Messagebox.EXCLAMATION ) == Messagebox.YES ){
			catModel.removeValue( series, category );
			selectedArea = null;
			refreshDB();
		}		
	}
	
	/*  Insert data into chart  */
	public void insert(){
		Comparable category = ( ( Textbox )Path.getComponent( "/DG_CINSERT/DG_IM_CTB" ) ).getValue();
		Comparable series = ( ( Textbox )Path.getComponent( "/DG_CINSERT/DG_IM_STB" ) ).getValue();
		Number value = new Double( ( ( Textbox )Path.getComponent( "/DG_CINSERT/DG_IM_VTB" ) ).getValue() );
		validateInput( category, series, value );
		catModel.setValue( category, series, value );
		selectedArea = null;
		DG_CINSERT.detach();
		refreshDB();
	}
	
	/*  Show modify dialog  */
	public void modifyDialog(){
		final Window winMod = ( Window )Executions.createComponents( "components/CatModify.zul", null, null );
		( ( Label )winMod.getFellow( "DG_MOSER" ) ).setValue( selectedArea.getAttribute( "series" ) );
		( ( Label )winMod.getFellow( "DG_MOCAT" ) ).setValue( selectedArea.getAttribute( "category" ) );
		( ( Label )winMod.getFellow( "DG_MOVAL" ) ).setValue( selectedArea.getAttribute( "value" ).toString() );
		winMod.setSizable( false );
		winMod.setAttribute( "commander", this );
		winMod.doModal();
	}
	
	/*  Update chart  */
	public void modify(){
		Number value = new Double( ( ( Textbox )Path.getComponent( "/DG_CMODIFY/DG_MM_VTB" ) ).getValue() );
		validateInput( null, null, value );
		catModel.setValue( selectedArea.getAttribute( "series" ), selectedArea.getAttribute( "category" ), value );
		selectedArea = null;
		DG_CMODIFY.detach();
		refreshDB();
	}
	
	/*  Process popup menu open event  */
	public void openPopup( OpenEvent event ){
		if( !event.isOpen() ){
			Menupopup popup = ( Menupopup )getParent().getFellow( "DG_CPOP" );
			if( !onArea ){
				( ( Menuitem )popup.getFellow( "DG_CPOP_DETAIL" ) ).setDisabled( true );
				( ( Menuitem )popup.getFellow( "DG_CPOP_INSERT" ) ).setDisabled( false );
				( ( Menuitem )popup.getFellow( "DG_CPOP_MODIFY" ) ).setDisabled( true );
				( ( Menuitem )popup.getFellow( "DG_CPOP_REMOVE" ) ).setDisabled( true );
			}
		}else{
			onArea = false;
		}
	}
	
	/*  Validate insert input  */
	public void validateInput( Comparable category, Comparable series, Number value ){
		
	}
	
	/*  Refresh database  */
	public void refreshDB(){
		
	}
	
	public void setVariable( String name, Object value, boolean local ){
		if( name.equals( "data" ) ){
			dataMap = ( Map )value;
			Pair keyPair;
			Iterator keyIter = dataMap.keySet().iterator();
			Iterator valIter = dataMap.values().iterator();
			while( keyIter.hasNext() ){
				keyPair = ( Pair )keyIter.next();
				catModel.setValue( keyPair.getFirst(), keyPair.getSecond(), valIter.next() );
			}
			return;
		}else{
			super.setVariable( name, value, local );
		}
	}
}
